package au.com.iglootech.TaxTimeToolTip;

import java.security.Principal;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Configurable;

import au.com.iglootech.TaxTimeToolTip.Entity.User;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;
import com.vaadin.service.ApplicationContext.TransactionListener;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Link;
import com.vaadin.ui.Window;

@Configurable(preConstruction = true)
public class TaxTimeToolTipApplication extends Application {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1346050755902298823L;
	private transient static HashMap<ApplicationContext, TransactionListener> requestListeners = new HashMap<ApplicationContext, TransactionListener>();
	private boolean isInitialized = false;
	private Window mainWindow;
	

	@Override
	public void init() {
		final UserSessionData sessionData = new UserSessionData(this);
		if (getContext() != null && requestListeners.get(getContext()) == null) {
			TransactionListener listener = new TransactionListener() {
				private static final long serialVersionUID = -8490216576436646032L;

				@Override
				public void transactionStart(Application app, Object req) {

					sessionData.transactionStart(TaxTimeToolTipApplication.this);
					checkLoginLogout((HttpServletRequest) req);
				}

				@Override
				public void transactionEnd(Application app, Object req) {
					sessionData.transactionEnd(TaxTimeToolTipApplication.this);
				}
			};

			getContext().addTransactionListener(listener);

			requestListeners.put(getContext(), listener);
		}
	}

	public void initContent(Principal user) {
		mainWindow = new TaxTimeToolTipMainWindow("Tax Time Sheet");
		setTheme("reindeermods");
		setMainWindow(mainWindow);
		((TaxTimeToolTipMainWindow)mainWindow).setUserInfo(user.getName());
	}

	private void checkLoginLogout(HttpServletRequest request) {
		Principal user = request.getUserPrincipal();

		if ((user != null)) {
			if(user.toString().equalsIgnoreCase("test@example.com") 
					|| user.toString().equalsIgnoreCase("nichols.zu@gmail.com")
					|| user.toString().equalsIgnoreCase("friendlc@gmail.com")){
				User userData = new User();
				userData.setUsername(user.getName());
				UserSessionData.setUserData(userData);
				if (!isInitialized) {
					initContent(user);
					isInitialized = true;
				}
//				initContent(user);
				return;
			}
		}

		UserService userService = UserServiceFactory.getUserService();
		mainWindow = new LoginWindow();
		((LoginWindow) mainWindow).addLink(new Link("login",
				new ExternalResource(userService.createLoginURL("http://taxtimesheet.appspot.com"))));
//		((LoginWindow) mainWindow).addLink(new Link("login",
//				new ExternalResource(userService.createLoginURL("http://localhost:7916/"))));
		setTheme("reindeermods");
		setMainWindow(mainWindow);
		// }

	}

}
